package com.hl.service;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hl.domain.User;
import com.hl.mapper.UserMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@RestController
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    //    查询全部
    public HashMap<String, Object> findAll(int page, int size) {
        //设置分页
        PageHelper.startPage(page, size);
        //查询全部数据
        List<User> list = userMapper.selectAllUser();
        PageInfo<User> pageInfo = new PageInfo<User>(list);
        //获取总数
        int total = (int) pageInfo.getTotal();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("list", list);
        map.put("total", total);
        return map;
    }
    //    条件查询
    public HashMap<String, Object> findByCondition(int page, int size, String username, String nickname) {
        // 设置分页
        PageHelper.startPage(page, size);
        // 查询数据
        List<User> list = userMapper.selectCount(username, nickname);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        // 获取总数
        long total = pageInfo.getTotal();
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("total", total);
        return map;
    }

    //    添加数据
    public int addUser(User user) {
        int insert = userMapper.insert(user);
        return insert;
    }

    //    改
    public int updateUser(User user) {
        int update = userMapper.updateByPrimaryKeySelective(user);
        return update;
    }

    //    删除数据
    public int deleteUser(Long id) {
        int delete = userMapper.deleteByPrimaryKey(id);
        return delete;
    }

   //根据用户名查询用户
    public User selectUserLogin(String username,String password){
        User user = userMapper.selectByUsernameUser(username);
        //用户不存在
        if (user == null){
            return null;
        }else if (password.equals(user.getPassword())){
            //用户存在密码正确
            return user;
        }else{
            //密码不正确
            return null;
        }

    }

	public List<User> selectUserAndConsultReply(int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        return userMapper.selectUserAndConsultReply();
    }

 public List<User> login(String username, String password){
        return userMapper.login(username,password);
    }

    //小程序注册
    public int register(User user){
        return userMapper.insert(user);
    }

    //根据电话号查询
    public List<User> selectByNumber(String phone){
        return userMapper.selectByNumber(phone);
    }

    //根据用户名或者手机号查询
    public User selectNameOrNumber(String username){
        return userMapper.selectNameOrNumber(username);
    }

    //根据用户名更改手机号
    public int updatePhoneByName(String username,String phone){
        return userMapper.updatePhoneByName(username,phone);
    }

    //根据用户名查找密码
    public int selectPwdByUsername(String username,String password){
        return userMapper.selectPwdByUsername(username,password);
    }

    //根据用户名修改密码
    public int updatePwdByUserNumber(String username,String password){
        return userMapper.updatePwdByUserNumber(username,password);
    }

    //根据用户名查找id
    public List<User> selectIdByUsername(String username){
        return userMapper.selectIdByUsername(username);
    }
}
